//
//  MSStoreNode.m
//  MagicSword
//
//  Created by Netease on 15/11/18.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSStoreNode.h"
#import "MSWeaponNode.h"
#import "MSWeaponManager.h"

@implementation MSStoreNode {
    SKSpriteNode *_weaponNode1;
    SKSpriteNode *_weaponNode2;
    SKSpriteNode *_weaponNode3;
    MSHeroInfo *_heroInfo;
}

- (id)init {
    self = [super initWithColor:UIColorFromRGBA(0x1e376f, 0.66) size:CGSizeMake(ScreenWidth/4, ScreenHeight)];
    if (self) {
        self.zPosition = 2;
        [self initSubNodes];
    }
    return self;
}

- (void)initSubNodes {
    float width = (CGRectGetWidth(self.frame)-4)/3.;
    float space = 1;
    float left = width/2-CGRectGetWidth(self.frame)/2+space;
    float top = CGRectGetHeight(self.frame)/2-width/2-44+space;
    
    _weaponNode1 = [SKSpriteNode spriteNodeWithImageNamed:@"weapon_background"];
    _weaponNode1.size = CGSizeMake(width, width);
    _weaponNode1.position = CGPointMake(left, top);
    _weaponNode1.zPosition = 3;
    [self addChild:_weaponNode1];
    
    _weaponNode2 = [SKSpriteNode spriteNodeWithImageNamed:@"weapon_background"];
    _weaponNode2.size = CGSizeMake(width, width);
    _weaponNode2.position = CGPointMake(left+width+space, top);
    _weaponNode2.zPosition = 3;
    [self addChild:_weaponNode2];
    
    _weaponNode3 = [SKSpriteNode spriteNodeWithImageNamed:@"weapon_background"];
    _weaponNode3.size = CGSizeMake(width, width);
    _weaponNode3.position = CGPointMake(left+(width+space)*2, top);
    _weaponNode3.zPosition = 3;
    [self addChild:_weaponNode3];
    
    [self initStoreWeaponArray];
}

- (void)initStoreWeaponArray {
    float width = (CGRectGetWidth(self.frame)-4)/3.;
    float space = 1;
    float left = width/2-CGRectGetWidth(self.frame)/2+space;
    float top = CGRectGetHeight(self.frame)/2-width/2-44+space;
    
    int i=0;
    for (MSWeaponInfo *info in [[MSWeaponManager shareManager] storeWeaponArray]) {
        MSWeaponNode *node = [[MSWeaponNode alloc] initWithWeaponInfo:info];
        node.size = CGSizeMake(width, width);
        [node setColor:UIColorFromRGBA(0xffffff, 0.3)];
        node.zPosition = 4;
        node.position = CGPointMake(left+((width+space)*(i%3)), (top-width-space)-((width+space)*(i/3)));
        node.name = NodeName_StoreWeaponNodes;
        [self addChild:node];
        i++;
    }
}

- (void)setHeroInfo:(MSHeroInfo *)heroInfo {
    _heroInfo = heroInfo;
    [self reloadView];
}

- (void)initHeroWeaponArray {
    int i = 0;
    for (MSWeaponInfo *info in _heroInfo.weaponList) {
        if (i<3) {
            MSWeaponNode *node = [[MSWeaponNode alloc] initWithWeaponInfo:info];
            node.size = _weaponNode1.size;
            node.zPosition = 4;
            node.name = NodeName_HeroWeaponNodes;
            [self addChild:node];
            if (i == 0) {
                node.position = _weaponNode1.position;
            } else if (i == 1) {
                node.position = _weaponNode2.position;
            } else if (i == 2) {
                node.position = _weaponNode3.position;
            }
            i++;
        }
    }
}

- (void)reloadView {
    [self removeAllChildren];
    
    [self addChild:_weaponNode1];
    [self addChild:_weaponNode2];
    [self addChild:_weaponNode3];
    [self initStoreWeaponArray];
    [self initHeroWeaponArray];
}

@end
